package com.joinway.framework.bean.utils;

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;

import com.joinway.framework.bean.logging.constant.MaskConstants;


public final class LoggingHelper {
    private final static int MIN_LEN = 4; 
//    private final static Set<Class<?>> SIMPLE_TYPES = new HashSet<>();
//
//    static {
//        SIMPLE_TYPES.add(Boolean.class);
//        SIMPLE_TYPES.add(Character.class);
//        SIMPLE_TYPES.add(Byte.class);
//        SIMPLE_TYPES.add(Short.class);
//        SIMPLE_TYPES.add(Integer.class);
//        SIMPLE_TYPES.add(Long.class);
//        SIMPLE_TYPES.add(Float.class);
//        SIMPLE_TYPES.add(Double.class);
//        SIMPLE_TYPES.add(String.class);
//    }

    private LoggingHelper() {}

    public static boolean isSimpleType(Class<?> type) {
        return ClassUtils.isPrimitiveOrWrapper(type) || type.isEnum() || type == String.class;
    }

    public static String markFullString(String str, String mask) {
        return str != null ? StringUtils.repeat(mask, str.length()) : str;
    }

    public static String markString(String ac) {
        return markString(ac, MaskConstants.MASK);
    }

    public static String markString(String input, String mask) {
    	String output = "";
    	
    	do{
    		if(input == null){
    			output = "<null>";
    			break;
    		}
    		
    		if(StringUtils.isBlank(input)){
    			output = "<" + input + ">";
    			break;
    		}
    		
    		if(input.length() <= MIN_LEN){
    			output = StringUtils.repeat(mask, MIN_LEN);
    			break;
    		}
    		
    		if(input.length() > MIN_LEN && input.length() <= MIN_LEN * 2){
    			output = input.substring(0, 2) + StringUtils.repeat(MaskConstants.MASK, input.length() - 2);
    			break;
    		}
    		
    		output = input.substring(0, MIN_LEN) + StringUtils.repeat(MaskConstants.MASK, input.length() - MIN_LEN);
    	}while(false);
    	
    	return output;
    }
}
